package com.dtjd.playcoinmonkey.activities;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivityModifyNicknameBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ModifyNicknameActivity extends AppCompatActivity {

    private ActivityModifyNicknameBinding binding;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityModifyNicknameBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.modifyNicknameBack.setOnClickListener(view -> finish());
        binding.modifyNicknameSave.setOnClickListener(view -> {
            if (!binding.modifyNicknameEt.getText().toString().equals("")){
                List<HttpParaBean> list=new ArrayList<>();
                list.add(new HttpParaBean("nickname",binding.modifyNicknameEt.getText().toString()));
                list.add(new HttpParaBean("token","token"));
                Network.post(list, "wbhMain/updateSimpleUserInfo", new Callback(){
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) {
                        runOnUiThread(() -> {
                            try {
                                JsonObject jo = JsonParser.parseString(response.body().string()).getAsJsonObject();
                                if (jo.get("code").getAsInt()==10066329){
                                    Tools.toast(jo.get("datalist").getAsString());
                                }else {
                                    Intent intent=new Intent();
                                    intent.putExtra("nickname",binding.modifyNicknameEt.getText().toString());
                                    setResult(3,intent);
                                    Tools.toast("修改成功");
                                }
                                finish();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        });
                    }
                });
            }else {
                Tools.toast("请输入昵称");
            }
        });
        binding.modifyNicknameEt.setText(MyApplication.user.wb_nickname);
        binding.modifyNicknameEt.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(binding.modifyNicknameEt, InputMethodManager.SHOW_IMPLICIT);
    }
}